﻿/*
 * This file is part of MonoSettlers.
 *
 * Copyright (C) 2010-2011 Christoph Husse
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: 
 *      # Christoph Husse
 * 
 * Also checkout our homepage: http://opensettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace MonoSettlers
{
    public enum WalkResult
    {
        Success,
        NotFound,
        Abort,
    }

    public static class GridSearch
    {
        /// <summary>
        /// Invokes the handler with values, walking along the whole grid in a rectangular
        /// spiral around start position. What exactly is the "grid" is within your power.
        /// </summary>
        /// <returns>
        /// Either WalkResult.Success or WalkResult.NotFound. If a handler returns WalkResult.Abort, then
        /// this method will return WalkResult.NotFound instead, but abort the search.
        /// </returns>
        /// <param name="inStartPos">An abstract point to start walking.</param>
        /// <param name="inWidth">The total width of the abstract grid.</param>
        /// <param name="inHeight">The total height of the abstract grid.</param>
        /// <param name="inHandler">A handler to be called for every encountered cell.</param>
        public static WalkResult GridWalkAround(Point inStartPos, Int32 inWidth, Int32 inHeight, Func<Point, WalkResult> inHandler)
        {
            Boolean hasField;
            Point center = inStartPos;
            Int32 radius = 1;

            if (inHandler(new Point(center.X, center.Y)) == WalkResult.Success)
                return WalkResult.Success;

            do
            {
                hasField = false;

                WalkResult res;
                Point start = new Point(
                    center.X + radius,
                    center.Y - radius);

                // walk down
                if ((center.X + radius >= 0) && (center.X + radius < inWidth))
                {
                    for (int y = center.Y - radius; y <= center.Y + radius; y++)
                    {
                        if (y < 0) continue;
                        if (y >= inHeight) break;

                        if ((res = inHandler(new Point(center.X + radius, y))) == WalkResult.Success)
                            return WalkResult.Success;

                        if (res == WalkResult.Abort)
                            return WalkResult.NotFound;

                        hasField = true;
                    }
                }

                // walk left
                if ((center.Y + radius >= 0) && (center.Y + radius < inHeight))
                {
                    for (int x = center.X + radius - 1; x >= center.X - radius; x--)
                    {
                        if (x < 0) break;
                        if (x >= inWidth) continue;

                        if ((res = inHandler(new Point(x, center.Y + radius))) == WalkResult.Success)
                            return WalkResult.Success;

                        if (res == WalkResult.Abort)
                            return WalkResult.NotFound;

                        hasField = true;
                    }
                }

                // walk up
                if ((center.X - radius >= 0) && (center.X - radius < inWidth))
                {
                    for (int y = center.Y + radius - 1; y >= center.Y - radius; y--)
                    {
                        if (y < 0) break;
                        if (y >= inHeight) continue;

                        if ((res = inHandler(new Point(center.X - radius, y))) == WalkResult.Success)
                            return WalkResult.Success;

                        if (res == WalkResult.Abort)
                            return WalkResult.NotFound;

                        hasField = true;
                    }
                }

                // walk right
                if ((center.Y - radius >= 0) && (center.Y - radius < inHeight))
                {
                    for (int x = center.X - radius + 1; x <= center.X + radius - 1; x++)
                    {
                        if (x < 0) continue;
                        if (x >= inWidth) break;

                        if ((res = inHandler(new Point(x, center.Y - radius))) == WalkResult.Success)
                            return WalkResult.Success;

                        if (res == WalkResult.Abort)
                            return WalkResult.NotFound;

                        hasField = true;
                    }
                }

                radius++;
            } while (hasField);

            return WalkResult.NotFound; // was not aborted by handler
        }
    }
}
